package com.vincent.exam.tree;

import java.util.ArrayList;
import java.util.List;

public class Orders {
    private List<Integer> first= new ArrayList<>();
    private List<Integer> mid= new ArrayList<>();
    private List<Integer> then= new ArrayList<>();
    /**
     *
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    public int[][] threeOrders (TreeNode root) {
        // write code here
        int[][] orders = new int[3][];
        firstOrder(root);
        midOrder(root);
        thenOrder(root);
        orders[0] = toArray(first);
        orders[1] = toArray(mid);
        orders[2] = toArray(then);
        return orders;
    }

    public int[] toArray(List<Integer> list){
        int[] array = new int[list.size()];
        for(int i = 0;i < list.size();i++){
            array[i] = list.get(i).intValue();
        }
        return array;
    }

    public void firstOrder(TreeNode node){
        if(node != null){
            first.add(node.val);
            firstOrder(node.left);
            firstOrder(node.right);
        }
    }

    public void midOrder(TreeNode node){
        if(node != null){
            midOrder(node.left);
            mid.add(node.val);
            midOrder(node.right);
        }
    }

    public void thenOrder(TreeNode node){
        if(node != null){
            thenOrder(node.left);
            thenOrder(node.right);
            then.add(node.val);
        }
    }
}
